home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3910 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.8 KB  |  102 lines

  1. Path: news.Stanford.EDU!usenet
  2. From: brien@leland.stanford.edu (brien oberstein)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Copy constructing an already default constructed object
  5. Date: Fri, 26 Jan 1996 15:28:46 GMT
  6. Organization: Stanford University
  7. Message-ID: <3108ef14.340699@nntp>
  8. References: <4e906b$stk@elaine32.Stanford.EDU> <4eal0n$hgq@dawn.mmm.com>
  9. NNTP-Posting-Host: tip-mp20-ncs-4.stanford.edu
  10.  
  11. On 26 Jan 1996 13:29:59 GMT, kjhopps@mmm.com (Kevin J Hopps) wrote:
  12.  
  13. >brien oberstein (brien@leland.Stanford.EDU) wrote:
  14. >> I'd like to get some opinions on the best/cleanest way
  15. >> to accomplish the following:
  16. >
  17. >> I've got an object which has already been constructed
  18. >> via its default constructor which just sets all pointers
  19. >> to NULL.  Whats the best way to deep-copy into it?
  20. >
  21. >[ example and discussion arriving at the conclusion that
  22. >  overloaded operator=() is required ]
  23. >
  24. >> //
  25. >> // deep-copy =
  26. >> //
  27. >> A& A::operator =(const A& other)
  28. >> {
  29. >>   A empty;
  30. >>   A tmp(other);
  31. >>   memcpy(this, &tmp, sizeof(A));
  32. >>   memcpy(&tmp, &empty, sizeof(A));
  33. >>   return *this;
  34. >> }
  35. >
  36. >
  37. >> I'd like to know what people think of the solution I've reached.
  38. >> I figure that this type of shit is common enough so there should 
  39. >> be some widely accepted solution to this problem.  Or maybe its
  40. >> not, but believe me that the situation does occur.
  41. >
  42. >You have arrived at the correct conclusion -- that operator=() is
  43. >what you are looking for.  However, the function you've coded has
  44. >some problems.
  45. >
  46. >First, you have to decide for yourself what the copy semantics of
  47. >your class are -- deep or shallow.  Regardless of what you decide,
  48. >I think that the copy constructor and the assignment operator
  49. >should produce identical results.  Frequently one is written in
  50. >terms of the other.  Ultimately they must do an exhaustive copy
  51. >of each data member.  Using memcpy as above can overwrite
  52. >compiler-generated data within the object and can have unpredictable
  53. >results.
  54. <snip>
  55.  
  56. What compiler generated data?  The vtable pointer?  What if I'm not
  57. using virtual functions?
  58.  
  59.  
  60. Ok.  Lemme give a better example of the actual problem I'm having.
  61. The object I have has other objects within it (which allocate
  62. dynamically), but no pointers are contained _directly_ in its class.
  63.  
  64. class A{
  65. public:
  66.     A();
  67.     A(const A&);
  68.     A(char *);
  69.  
  70. private:
  71.    B b0, b1;
  72. };
  73.  
  74. class B{
  75. public:
  76.   B();
  77.   B(const B&);
  78.   B(char *);
  79.  
  80. private:
  81.    char *p;
  82. }
  83.  
  84. So my copy constructor for A is easy:
  85. A::A(const A& other) : b0(other.b0), b1(other.b1) { }
  86.  
  87. Now how do you do the copy?  Do you have to
  88. overload = for all the contained operators too?
  89. ie,
  90.  
  91. A::operator =(const A& other)
  92. {
  93.   b0 = other.b0 ;
  94.   b1 = other.b1;
  95. }
  96.  
  97. To me this seems like a major pain in the butt.  All I really
  98. want to do is have the copy constructor invoked on the piece that
  99. piece of memory.  Why can't this be done?
  100.  
  101. -brien
  102.